export class Utils {
    static mergeObject(dst, src, makeClone = false){
        if(makeClone){
            let ret = {};
            for(let key in src){
                ret[key] = src[key];
            }
            for(let key in dst){
                if(!(key in ret)){
                    ret[key] = dst[key];
                }
            }
            return ret;
        } else {
            for(let key in src){
                dst[key] = src[key];
            }
            return dst;
        }
    }

    static cloneObject(obj){
        let ret = {};
        for(let key in obj){
            ret[key] = obj[key];
        }
        return ret;
    }

    // 使用反射来实现（暂时不用）
    static makeReflect(obj1, obj2){

    }

    static getFormData(obj){
        let formData = new FormData();
        for(let key in obj){
            formData.append(key, obj[key]);
        }
        return formData;
    }

    static getTime(offset = 0){
        return parseInt((new Date()).getTime() / 1000) + offset;
    }

    static checkIsMobile(){
        let userAgentInfo = navigator.userAgent;
        let Agents = ["Android", "iPhone",
                    "SymbianOS", "Windows Phone",
                    "iPad", "iPod"];
        let flag = false;
        for (let v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    static getQuery(){
        let queryString = location.search;
        if(queryString.length > 0 && queryString[0] === '?'){
            queryString = queryString.substr(1);
            let queryData = {};
            let queryList = queryString.split('&');
            queryList.forEach((data) => {
                let [key, value] = data.split('=');
                key = decodeURIComponent(key);
                value = decodeURIComponent(value);
                queryData[key] = value;
            });
            return queryData;
        } else {
            return {};
        }
    }
}